/*
 * Copyright 2013 Krzysztof Smigielski
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nilcaream.dirstore.core.common;

import javax.inject.Singleton;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * TODO
 * <p/>
 * Krzysztof Smigielski 3/27/13 9:47 PM
 */
@Singleton
public class ProgressMeter {

    private final Map<String, AtomicInteger> idToCurrent = Collections.synchronizedMap(new HashMap<String, AtomicInteger>());

    public void initialize(Object object) {
        idToCurrent.put(generateID(object), new AtomicInteger(0));
    }

    public void increment(Object object) {
        idToCurrent.get(generateID(object)).incrementAndGet();
    }

    public int getCurrent(Object object) {
        AtomicInteger current = idToCurrent.get(generateID(object));
        if (current == null) {
            return 0;
        } else {
            return current.intValue();
        }
    }

    private String generateID(Object object) {
        return object.getClass().toString() + object.hashCode();
    }
}
